﻿#include "BaseType.h"
#include "Tracer.h"
#include "SerialPort.h"
#include "Thread.h"
using namespace libemb;

#include <stdlib.h>

#define COM_PORT	"/dev/ttySAC4"


SerialPort* pCom=NULL;
static bool stopRecvFlag = false;
static int datamode = DATAMODE_RS232; 
class ComReciever:public Runnable{
public:
	void run()
	{
		sint32 ret;
        if(datamode == DATAMODE_RS485)
        {
            pCom->setAttribute(SERIAL_ATTR_RS485DIR,RS485DIR_RX); 
        }
		while(1)
		{
		    if (pCom!=NULL)
			{
			    char buf[64]={0};
        		ret=pCom->readData(buf, 63);
        		if(ret>0)
        		{
        			TRACE_DBG("COM recv:[%s]\n",buf);
        		}
                else
                {
        			//TRACE_ERR("Com recv error,ret=%d\n",ret);
                    usleep(1000);
        		}
            }
            if (stopRecvFlag)
            {
                TRACE_WARN("stop recv thread.\n");
                break;
            }
        }

	}
};


void TestComPort()
{
	sint32 ret;
	Thread recvThread;
	ComReciever comRecv;
    
    pCom = NEW_OBJ SerialPort();

    std::string inputStr;
    std::cout << "please input baud rate:" << std::endl;
    std::cin >> inputStr;
    std::cout << inputStr << std::endl;
	pCom->setAttribute(SERIAL_ATTR_BAUDRATE, atoi(inputStr.c_str()));

    std::cout << "please input tty device:" << std::endl;
    std::cin >> inputStr;
    std::cout << inputStr << std::endl;
	ret=pCom->open(inputStr.c_str(), IO_MODE_RDWR_ONLY);
	if(STATUS_ERROR==ret)
	{
		TRACE_ERR("open %s error:%s\n", COM_PORT, ERROR_STRING);
		return;
	}

    std::cout << "select mode: 0-rs232, 1-rs422, 2-rs485" << std::endl;
    std::cin >> inputStr;
    std::cout << inputStr << std::endl;
    datamode = atoi(inputStr.c_str());
    pCom->setAttribute(SERIAL_ATTR_DATAMODE, datamode);

    while (1) 
    {
        TRACE_YELLOW("-------------------------------\n");
        TRACE_YELLOW("01: start recv.\n");
        TRACE_YELLOW("02: stop recv.\n");
        TRACE_YELLOW("02: start send.\n");
        TRACE_YELLOW("q : quit.\n");
        TRACE_YELLOW("-------------------------------\n");
        std::cin >> inputStr;
        std::cout << inputStr << std::endl;
        if ("q" == inputStr || "quit" == inputStr) return;
        if ("01" == inputStr)
        {
            stopRecvFlag = false;
            recvThread.start(&comRecv);
        }
        else if ("02" == inputStr)
        {
            stopRecvFlag = true;
        }
        else if ("03" == inputStr)
        {
            TRACE_REL("input message you want to send:\n");
            std::cin >> inputStr;
            std::cout << inputStr << std::endl;
            if(datamode == DATAMODE_RS485)
            {
                pCom->setAttribute(SERIAL_ATTR_RS485DIR,RS485DIR_TX);
            }
            ret = pCom->writeData(inputStr.c_str(), inputStr.size()); 
            if(ret<=0)
            {
                TRACE_ERR("Com send error,ret=%d\n",ret);
            }
            else
            {
                TRACE_REL("Com send ok,ret=%d.\n",ret);
            }
        }
        else
        {
            TRACE_RED("Unknown selection:%s !\n",inputStr.c_str());
        }
        inputStr.clear();
    }
}